package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Override
    public void saveOrderSettingBatch(List<String[]> ordersettinglist) {
           //  mp    ordersettinglist---List<OrderSetting>
         //  1.  excel数据 转 集合对象
        List<OrderSetting> allOrderSettings = toOrderSettingList(ordersettinglist);
        //  2.  将allOrderSettings  数据  查询数据库 有没有相同数据记录
        for (OrderSetting allOrderSetting : allOrderSettings) {
              //  select  * from  where orderdate = '2020-10-14'
            QueryWrapper queryWrapper = new QueryWrapper();
            Date orderDate = allOrderSetting.getOrderDate();
            queryWrapper.eq("orderdate",orderDate);
            OrderSetting orderSettingIsExist = baseMapper.selectOne(queryWrapper);
            if(orderSettingIsExist!=null){
                allOrderSetting.setId(orderSettingIsExist.getId());
            }
        }
        saveOrUpdateBatch(allOrderSettings);
         //   直接调用 mp 完成数据 批量插入和 批量更新操作

    }

    @Override
    public Map findSettingData(int year, int month) {
        // 2000-2   1   -    28 29 30 31
        String beginDate = year+"-"+month+"-1"; //  2020-10-1
        String endDate = year+"-"+month+"-31"; //  2020-2-31
        //  Date      mysql
        //  select * from  xxx where  orderdate  between  2020-10-1  and  2020-2-31
//        QueryWrapper  queryWrapper = new QueryWrapper();
//        queryWrapper.between("orderdate",beginDate,endDate);
//        List<OrderSetting> list = baseMapper.selectList(queryWrapper); //  全字段的查询
        //  2 方案2   按需查询
         List<OrderSetting> orderSettings =  baseMapper.findOrderSettingsByOrderDate(beginDate,endDate);
        Map map  = new HashMap();
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            OrderSetting orderSetting1 = new OrderSetting();
            orderSetting1.setNumber(orderSetting.getNumber());
            orderSetting1.setReservations(orderSetting.getReservations());
            map.put(DateUtils.parseDate2String(orderDate,"yyyy-MM-dd"),orderSetting1);
        }
        return map;
    }

    @Override
    public void updateNumberByOrderDate(String orderdate, int number) {
        baseMapper.updateNumberByOrderDate(orderdate,number);
    }

    @Override
    public int isOrderOkByOrderDate(String orderDate) {

        return baseMapper.isOrderOkByOrderDate(orderDate);
    }

    @Override
    public void updateReservationsByOrderDate(String orderDate) {
        baseMapper.updateReservationsByOrderDate(orderDate);
    }

    /**
     *    集合数组 转换成  集合对象
     * @param ordersettinglist
     * @return
     */
    private List<OrderSetting> toOrderSettingList(List<String[]> ordersettinglist) {

        if(ordersettinglist!=null){
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] strings : ordersettinglist) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setReservations(0);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSettings.add(orderSetting);
            }
            return  orderSettings;
        }

        return null;
    }

    @Override
    public void deleteFile(String date) {
        baseMapper.deleteFile(date);
    }

    @Override
    public void UpdateIsDatelet(String date) {
        baseMapper.UpdateIsDatelet(date);
    }


}



